package handler

import (
	"lnzw/common/proto/cs_gen"
	"lnzw/engine/actor"
	"lnzw/server/test/test_fyne/test_3/data"
	"strconv"

	"google.golang.org/protobuf/proto"
)

func ReqCreateMap(req proto.Message, rsp proto.Message) {
	reqParams := req.(*cs_gen.ReqCreateMap)
	rspRs := rsp.(*cs_gen.ResCreateMap)
	width := reqParams.Width
	height := reqParams.Height
	mapmgr := actor.GetActor(data.MapMgrCatalog).(*data.MapMgr)
	if mapmgr == nil {
		rspRs.Result = cs_gen.ErrorCode_ERR_SYS_ERROR
		return
	}
	mapId := mapmgr.CreateMap(int(width), int(height))
	rspRs.MapId = mapId
}

func ReqMapInfo(req proto.Message, rsp proto.Message) {
	reqParams := req.(*cs_gen.ReqMapInfo)
	rspRs := rsp.(*cs_gen.ResMapInfo)
	mapId := reqParams.MapId
	mapActor := actor.GetActor(strconv.FormatInt(mapId, 10))
	if mapActor == nil {
		rspRs.Result = cs_gen.ErrorCode_ERR_NOT_FOUND_MAP
		return
	}
	mapInfo := mapActor.(*data.Map)
	rspRs.MapInfo = &cs_gen.MapInfo{
		Width:    int32(mapInfo.GetWidth()),
		Height:   int32(mapInfo.GetHeight()),
		CellSize: int32(mapInfo.GetCellSize()),
		MapId:    mapId,
	}
}

func ReqMapList(req proto.Message, rsp proto.Message) {
	//reqParams := req.(*cs_gen.ReqMapList)
	rspRs := rsp.(*cs_gen.ResMapList)
	mapmgr := actor.GetActor(data.MapMgrCatalog).(*data.MapMgr)
	if mapmgr == nil {
		rspRs.Result = cs_gen.ErrorCode_ERR_SYS_ERROR
		return
	}
	rspRs.MapIds = mapmgr.GetMapListIds()
}

func ReqMapUnitInfo(req proto.Message, rsp proto.Message) {
	reqParams := req.(*cs_gen.ReqMapUnitInfo)
	rspRs := rsp.(*cs_gen.ResMapUnitInfo)
	mapId := reqParams.MapId
	mapActor := actor.GetActor(strconv.FormatInt(mapId, 10))
	if mapActor == nil {
		rspRs.Result = cs_gen.ErrorCode_ERR_NOT_FOUND_MAP
		return
	}
	mapInfo := mapActor.(*data.Map)
	rspRs.MonsterNum = int32(mapInfo.GetMonsterNum())
	rspRs.PlayerNum = int32(mapInfo.GetPlayerNum())
	rspRs.UnitsNum = int32(mapInfo.UnitsNum())
}
